package com.nova.android.tvlauncher.utils.keyboard

import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.atv_bean.KeyBoardDto
import com.nova.android.tvlauncher.bean.atv_bean.KeyBoardDto.Companion.KEYBOARD_TYPE_LETTER
import com.nova.android.tvlauncher.bean.atv_bean.KeyBoardDto.Companion.KEYBOARD_TYPE_SWITCH_Special_Characters
import java.util.Locale

/**
 * 纯工具类，不依赖 ViewModel，不依赖 LiveData
 * 所有逻辑与你之前 KeyBoardViewModel 完全一致
 */
object KeyBoardHelper {

    fun addNumChar(): MutableList<KeyBoardDto> {
        val numList = mutableListOf<KeyBoardDto>()
        val list = arrayListOf("1", "2", "3", "4", "5", "6", "7", "8", "9", ".com", "0", "@")
        list.forEach {
            numList.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_NUM, it, null))
        }
        return numList
    }

    /**
     * 构建小写/大写字母键盘
     */
    fun addLowerCase(isUpper: Boolean): MutableList<KeyBoardDto> {
        val keyBoardDtos = mutableListOf<KeyBoardDto>()

        arrayListOf("q", "w", "e", "r", "t", "y", "u").forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, it, null))
        }

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_CASE,
                null,
                R.drawable.baseline_lower_cast_100
            )
        )

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_DEL,
                null,
                R.drawable.baseline_backspace_100
            )
        )

        // 第二排 a-s-d-f-g-h-j-OK
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "a", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "s", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "d", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "f", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "g", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "h", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "j", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "m", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_OK, null, R.drawable.icon_done))

        // 第三排 z-x-c-v-b-n-i-o-?123
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "z", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "x", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "c", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "v", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "b", null))
        keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, "n", null))
        keyBoardDtos.add(KeyBoardDto(KEYBOARD_TYPE_LETTER, "i", null))
        keyBoardDtos.add(KeyBoardDto(KEYBOARD_TYPE_LETTER, "o", null))
        keyBoardDtos.add(KeyBoardDto(KEYBOARD_TYPE_SWITCH_Special_Characters, "?123", null))


        arrayListOf("p", "k", "l").forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_LETTER, it, null))
        }

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_LEFT_ARROW,
                null,
                R.drawable.keyboard_left_arrow
            )
        )

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_RIGHT_ARROW,
                null,
                R.drawable.keyboard_right_arrow
            )
        )

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_SPACE,
                null,
                R.drawable.keyboard_space
            )
        )

        // 大写切换
        if(isUpper) {
            keyBoardDtos.forEach {
                if (it.keyboardType == KeyBoardDto.KEYBOARD_TYPE_LETTER) {
                    it.keyboardChar = it.keyboardChar?.uppercase(Locale.getDefault())
                }
                if (it.keyboardType == KeyBoardDto.KEYBOARD_TYPE_CASE) {
                    it.keyboardIcon = R.drawable.baseline_up_case_100
                }
            }
        }

        return keyBoardDtos
    }

    /**
     * 构建符号键盘
     */
    fun addHalfWidth(): MutableList<KeyBoardDto> {
        val keyBoardDtos = mutableListOf<KeyBoardDto>()

        arrayListOf("~", "!", "#", "$", "^", "%", "&", "`").forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_HALF_CHAR, it, null))
        }

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_DEL,
                null,
                R.drawable.baseline_backspace_100
            )
        )

        arrayListOf("?", "(", ")", "_", "+", "=", "*", ";").forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_HALF_CHAR, it, null))
        }

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_BACK_LETTER,
                null,
                R.drawable.icon_back
            )
        )

        arrayListOf(":", "'", "\"", "-", "<", ">",).forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_HALF_CHAR, it, null))
        }

        arrayListOf(",", ".", "[", "]", "/", "\\", "￥", "|").forEach {
            keyBoardDtos.add(KeyBoardDto(KeyBoardDto.KEYBOARD_TYPE_HALF_CHAR, it, null))
        }

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_LEFT_ARROW,
                null,
                R.drawable.keyboard_left_arrow
            )
        )

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_RIGHT_ARROW,
                null,
                R.drawable.keyboard_right_arrow
            )
        )

        keyBoardDtos.add(
            KeyBoardDto(
                KeyBoardDto.KEYBOARD_TYPE_OK,
                null,
                R.drawable.icon_done
            )
        )

        return keyBoardDtos
    }

    /**
     * 大小写切换（保持你原来的 callback 行为）
     */
    fun switchLowerUpper(
        adapterList: MutableList<KeyBoardDto>,
        isUpper: Boolean,
        callback: ((index: Int) -> Unit)? = null
    ) {
        val list = mutableListOf<KeyBoardDto>()

        // 深复制
        adapterList.forEach {
            val dto = KeyBoardDto(it.keyboardType, it.keyboardChar, it.keyboardIcon)
            list.add(dto)

            if (dto.keyboardType == KeyBoardDto.KEYBOARD_TYPE_LETTER) {
                dto.keyboardChar =
                    if (isUpper) dto.keyboardChar?.uppercase(Locale.getDefault())
                    else dto.keyboardChar?.lowercase(Locale.getDefault())
            }

            if (dto.keyboardType == KeyBoardDto.KEYBOARD_TYPE_CASE) {
                dto.keyboardIcon =
                    if (isUpper) R.drawable.baseline_up_case_100
                    else R.drawable.baseline_lower_cast_100
            }
        }

        // 对比差异
        list.forEachIndexed { parentIndex, dto ->
            adapterList.forEachIndexed { index, keyBoardDto ->
                if (parentIndex == index && dto.keyboardChar != keyBoardDto.keyboardChar) {
                    keyBoardDto.keyboardChar = dto.keyboardChar
                    callback?.invoke(index)
                }
                if (parentIndex == index && dto.keyboardIcon != keyBoardDto.keyboardIcon) {
                    keyBoardDto.keyboardIcon = dto.keyboardIcon
                    callback?.invoke(index)
                }
            }
        }
    }
}
